package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.pojo.dto.*;
import cn.tedu.fruitshop.pojo.vo.ItemListItemVO;
import cn.tedu.fruitshop.pojo.vo.ItemStandardVO;
import cn.tedu.fruitshop.service.impl.ItemServiceImpl;
import cn.tedu.fruitshop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.File;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/item")
@Api(tags = "02. 商品管理模块")
@Validated
public class ItemController {
    @Value("${dirPath}")
    private String dirPath;

    @Autowired
    ItemServiceImpl itemService;


    public ItemController() {
        log.debug("创建控制器对象：CarController");
    }


    @ApiOperation("添加商品")
    @ApiOperationSupport(order = 130)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid ItemAddNewDTO itemAddNewDTO) {
        log.debug("开始处理【添加商品】的请求，参数：{}", itemAddNewDTO);
        itemService.addNew(itemAddNewDTO);
        log.debug("添加商品成功！");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除商品")
    @ApiOperationSupport(order = 131)
    @ApiImplicitParam(name = "id", value = "评论id", required = true, dataType = "Integer")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除商品失败，尝试删除的商品的ID无效！")
                                   @PathVariable Integer id) {
        log.debug("开始处理【根据id删除商品】的请求，参数：{}", id);
        itemService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据商品分类id以及页数查询商品列表")
    @ApiOperationSupport(order = 132)
    @GetMapping
    public JsonResult<List<ItemListItemVO>> list(Integer cid, Integer page) {
        page = (page - 1) * 2;
        log.debug("开始查询商品列表请求,参数为:{}", cid, page);
        List<ItemListItemVO> list = itemService.list(cid, page);
        return JsonResult.ok(list);
    }

    @ApiOperation("查询商品列表")
    @ApiOperationSupport(order = 133)
    @GetMapping("/list")
    public JsonResult<List<ItemListItemVO>> listAll() {
        log.debug("开始查询商品列表请求,无参数");
        List<ItemListItemVO> list = itemService.listAll();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据id修改商品详情")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Integer")
    @PostMapping("/{id:[0-9]+}/updateById")
    public JsonResult<Void> updateById(@PathVariable Integer id, ItemUpdateInfoDTO itemUpdateInfoDTO) {
        log.debug("开始处理【修改商品详情】的请求，参数ID：{}, 新数据：{}", id, itemUpdateInfoDTO);
        itemService.updateInfoById(id, itemUpdateInfoDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据商品名称查询商品列表")
    @ApiOperationSupport(order = 1322)
    @GetMapping("/selectByWd")
    public JsonResult<List<ItemListItemVO>> selectByWd(String name) {
        log.debug("开始查询商品列表请求,参数为:{}", name);
        List<ItemListItemVO> list = itemService.selectByWd(name);
        return JsonResult.ok(list);
    }

    @ApiOperation("根据商品id查询商品详情")
    @ApiOperationSupport(order = 132222)
    @GetMapping("/getStandardById")
    public JsonResult getStandardById(Integer id) {
        log.debug("开始查询商品详情请求,参数为:{}", id);
        ItemStandardVO standardById = itemService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @ApiOperation("修改商品详情")
    @ApiOperationSupport(order = 3000)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable int id, ItemUpdateInfoDTO itemUpdateInfoDTO) {
        log.debug("开始处理【修改商品详情】的请求，参数ID：{}, 新数据：{}", id, itemUpdateInfoDTO);
        itemService.updateInfoById(id, itemUpdateInfoDTO);
        return JsonResult.ok();
    }

    @ApiOperation("启用商品")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "int")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Integer id) {
        log.debug("开始处理【启用商品】的请求，参数：{}", id);
        itemService.setEnable(id);
        return JsonResult.ok();
    }


    @ApiOperation("禁用商品")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Integer id) {
        log.debug("开始处理【禁用商品】的请求，参数：{}", id);
        itemService.setDisable(id);
        return JsonResult.ok();
    }

}
